package com.atguigu.gulimall.product.web;

import com.atguigu.gulimall.product.entity.CategoryEntity;
import com.atguigu.gulimall.product.service.CategoryService;
import com.atguigu.gulimall.product.vo.Catelog2Vo;
import org.redisson.api.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import java.rmi.server.ExportException;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @Author: 风鹤
 * @Date: 2020/11/16 16:36
 */
@Controller
public class IndexController {

    @Autowired
    CategoryService categoryService;
    @Autowired
    RedissonClient reidsson;
    @Autowired
    StringRedisTemplate stringRedisTemplate;
    @GetMapping({"/","index.html"})
    public String indexPage(Model model){
        //1、查出所有的一级分类
        List<CategoryEntity> categoryEntities =categoryService.getLevel1Categorys();
        model.addAttribute("categories",categoryEntities);
        return "index";
    }

    @GetMapping(value = "/index/catalog.json")
    @ResponseBody
    public Map<String, List<Catelog2Vo>> getCatalogJson() {

        Map<String, List<Catelog2Vo>> catalogJson = categoryService.getCatalogJson();

        return catalogJson;

    }
    @GetMapping("/hellow")
    @ResponseBody
    public String hellow(){
        // 获取锁 名字一样 就是同一把锁
        RLock lock = reidsson.getLock("my-locks");
        //2.加锁
        lock.lock();//阻塞式等待 默认加的锁都是30秒过期
        // 如果业务时间过长，会自动给锁续期加上新的30秒 ，不用担心操作过长锁过期
        try{
            System.out.println("枷锁成功，执行业务"+Thread.currentThread().getId());
            Thread.sleep(30000);
        }catch (Exception e){
        }finally {
            // 假设解锁
            System.out.println("解锁操作"+Thread.currentThread().getId());
            lock.unlock();
        }
        return  "hellow";
    }
    // 读写锁 保证能读到最新数据
    // 写锁没释放 读必须等待
// 写锁 是排他锁 读锁共享锁
    /**
     * 读+读  同时读 相当于没锁
     * 写+读  等待写锁释放
     * 写+写  排队等候 阻塞方式
     * 读+写： 写等待
     * @return
     */
    @GetMapping("/write")
    @ResponseBody
    public String writeValue(){
        RReadWriteLock lock = reidsson.getReadWriteLock("rw-lock");
        // 1.该数据加些所，读数据加读锁
        RLock rLock = lock.writeLock();
        rLock.lock();
        String s="";
        try {
            s = UUID.randomUUID().toString();
            Thread.sleep(30000);
            stringRedisTemplate.opsForValue().set("writeValue",s);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            rLock.unlock();
        }
        return s;

    }

    @GetMapping("/read")
    @ResponseBody
    public String readValue(){
        RReadWriteLock lock = reidsson.getReadWriteLock("rw-lock");
        RLock rLock = lock.readLock();
        rLock.lock();
        String s="";
        try {
           s= stringRedisTemplate.opsForValue().get("writeValue");
            Thread.sleep(30000);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            rLock.unlock();
        }
        return s;

    }

    @GetMapping("/park")
    @ResponseBody
    public String park() throws InterruptedException {
        RSemaphore park = reidsson.getSemaphore("park");
        //park.acquire();//获取信号值
        boolean b = park.tryAcquire();
            if(b){
                System.out.println("业务执行");

            }else{
                return "error";
            }
            return "OK=>"+b;
    }

    @GetMapping("/go")
    @ResponseBody
    public String go() throws InterruptedException {
        RSemaphore park = reidsson.getSemaphore("park");
        park.release();//释放信号值

        return "OK";
    }

    @GetMapping("/door")
    @ResponseBody
    public String lookDoor() throws InterruptedException {
        RCountDownLatch door = reidsson.getCountDownLatch("door");
        door.trySetCount(5);
        door.await();
        return "放假";
    }


    @GetMapping("/list")
    @ResponseBody
    public String gogo(Integer id) throws InterruptedException {
        RCountDownLatch door = reidsson.getCountDownLatch("door");
        door.countDown();
        return id+"班人走了";
    }
}
